/**
 * Created by jt on 2017/5/6.
 */
import "./index.less";
import "../../../unit/common/js/bootstrap_js/select2.min.js";
import "../../../unit/common/js/bootstrap_js/jquery.dataTables.js";
import "../../../unit/common/js/bootstrap_js/form-samples.js";
import "../../../unit/common/js/bootstrap_js/table-editable.js";

import {dpstSts,bidDocSts} from "../config.js";
import bidListTpl from "./getbid.handlebars";
import paginationTpl from "./paging.handlebars";

var token = sessionStorage.getItem('token');
if (token == null || token =='') {
    location.href = HOST_FRT+'/logins/login.html';
}
var projId = getArgs().id;
let data={"projId": projId};

var bidList = {
    pageNo: 1,
    pageSize: 20,
    maxPage: 1,
    isFirstPage: false,
    isLastPage: false,
    total: 0,
    init: function(){
        this.getBidData()
            .then(this.renderbidList)
            .then(this.renderPagination)
            .then(this.bindEvent);
    },
    getBidData: function() {
        return new Promise((resolve, reject)=>{
                let self = this;
        $.ajax({
            url: HOST_ADDR + `/cpmp/bidProjOff/queryBidProjs/${self.pageNo}/${self.pageSize}/`,
            type: 'post',
            dataType: 'json',
            headers: {
                'access_token': token,'login_type':'ums'
            },
            data:JSON.stringify(data),
            contentType: "application/json",
            success: function (resp) {
                if (resp.success) {
                    console.log(resp);
                    resolve(resp);
                }
                else {
                    alert(resp.msgInf);
                }
            },
            error: function (err) {
                console.log(err);
            }
        });

    });

    },
    // 渲染列表
    renderbidList: function(resp){
        let self = bidList;
        // 处理数据begin
        self.total = resp.data.count;
        self.isFirstPage = self.pageNo === 1;
        self.maxPage = Math.ceil(self.total / self.pageSize);
        $.each(resp.data.list,function(index,item){
            item.dpstStsFmt = dpstSts[item.dpstSts];
            item.dpstPicFmt = HOST_FILE+item.dpstPic;
            item.payAvailabel = (item.dpstSts == '1');

            item.bidDocStsFmt = bidDocSts[item.bidDocSts] || '暂无状态';
            item.memoFmt = item.memo || '';

            item.passAvailabel = (item.bidDocSts == '00');
            item.refuseAvailabel = (item.bidDocSts == '00');
            item.operateAvailabel = (item.bidDocSts != '00');
        });
        // 处理数据end
        if(self.pageNo * self.pageSize >= self.total){
            self.isLastPage = true;
        }else{
            self.isLastPage = false;
        }

        let resultHtml = bidListTpl({bidList: resp.data.list});
        $('tbody.list').html(resultHtml);
    },

    // 渲染分页
    renderPagination: function(){
        let self = bidList;
        let pageList = [];
        for(let i=0; i<self.maxPage; i++){
            pageList.push({idx: i+1});
        }
        console.log(self.total);
        let paginationHtml = paginationTpl({
            begin: (self.pageNo-1) * self.pageSize + 1,
            end: self.isLastPage ? self.total : self.pageNo * self.pageSize,
            total: self.total,
            isLastPage: self.isLastPage,
            isFirstPage: self.isFirstPage,
            pageList: pageList

        });
        $('.J_pagination').html(paginationHtml);
        $('.page-item').each((index,item)=>{
            if($(item).data('index')===self.pageNo){
            console.log(this);
            $(item).addClass('active');
        }
            //最多显示7个页码
            if (Math.abs($(item).data('index')-self.pageNo) > 3) {
                $(item).css('display','none');
            }

        });
    },

    // 绑定事件
    bindEvent: function(){
        let self = bidList;
        $('.J_prev').unbind().click(function(){
            if(self.pageNo > 1){
                self.pageNo = self.pageNo -1;
                self.renderList();
            }
        });
        $('.J_next').unbind().click(function(){
            if(self.pageNo < self.maxPage){
                self.pageNo = self.pageNo + 1;
                self.renderList();
            }

        });

        //首尾页
        $('.isFirst').unbind().click(function(){
            if(self.pageNo != 1){
            self.pageNo = 1;
            self.renderList();
            }
        });
        $('.isLast').unbind().click(function(){
            if(self.pageNo != self.maxPage){
                self.pageNo = self.maxPage;
                self.renderList();
            }
        });

        $('.page-item').unbind().click(function(){
            let $item = $(this);

            if(!$item.hasClass('active')){
                self.pageNo = $item.data('index');
                self.renderList();
            }
        });

        //通过
        jQuery('.passBut').click(function () {
            var _this = jQuery(this);
            var sid = _this.parent('td').siblings('td.sid').text();

            jQuery('.passReason').click(function () {
                if (jQuery('.passInfo').val() != '') {
                    let data = {
                        "id": sid,
                        "adtMemo": '',
                        "bidDocSts": '01'
                    };
                    console.log(data)
                    jQuery.ajax({
                        url: HOST_ADDR+'/cpmp/bidProjOff/adtBidInf',
                        type: 'post',
                        data: JSON.stringify(data),
                        dataType: 'json',
                        contentType: "application/json",
                        headers: {
                            'access_token': token,'login_type':'ums'
                        },
                        success: function (resp) {
                            if (!resp.success) {
                                alert(resp.msgCod + ' ' + resp.msgInf);
                                return;
                            }
                            console.log(resp);
                            location.reload();

                        },
                        error: function (err) {
                            alert(err);
                        }
                    });
                } else {
                    jQuery(this).attr({'data-toggle': 'modal', 'href': '#passReason'});
                }
            });
        });
        //拒绝
        jQuery('.refuseBut').click(function () {
            var _this = jQuery(this);
            var sid = _this.parent('td').siblings('td.sid').text();

            jQuery('.refuseReason').click(function () {
                if (jQuery('.refuseInfo').val() != '') {
                    let data = {
                        "id": sid,
                        "adtMemo": jQuery('.refuseInfo').val(),
                        "bidDocSts": '02'
                    };
                    // console.log(data)
                    jQuery.ajax({
                        url: HOST_ADDR+'/cpmp/bidProjOff/adtBidInf',
                        type: 'post',
                        data: JSON.stringify(data),
                        dataType: 'json',
                        contentType: "application/json",
                        headers: {
                            'access_token': token,'login_type':'ums'
                        },
                        success: function (resp) {
                            if (!resp.success) {
                                alert(resp.msgCod + ' ' + resp.msgInf);
                                return;
                            }
                            console.log(resp);
                            location.reload();

                        },
                        error: function (err) {
                            alert(err);
                        }
                    });
                } else {
                    jQuery(this).attr({'data-toggle': 'modal', 'href': '#refuseReason'});
                }
            });
        });
    },

    // 渲染列表
    renderList(){
        this.getBidData()
            .then(this.renderbidList)
            .then(this.renderPagination)
            .then(this.bindEvent);
    }
};

App.init();
bidList.init();
jQuery('.logOut').attr('href','../logins/login.html');